package net.dgg.zz.common.entity;

import com.alibaba.fastjson.annotation.JSONField;

import java.beans.Transient;
import java.util.Map;

public class PageList<T> {
    /**
     * 当前页
     */
    private int currentPage = 1;

    /**
     * 每页数据
     */
    private int pageSize = 10;

    /**
     * 上一页
     */
    private int upPage;

    /**
     * 下一页
     */
    private int nextPage;

    /**
     * 总页数
     */
    private int totalPage;

    /**
     * 总数据量
     */
    private int totalCount = 0;

    /**
     * 数据对象
     */
    private T data;

    /**
     * 所有集合
     */
    private java.util.List<T> records = new java.util.ArrayList<>();

    /**
     * 排序字段
     */
    private String orderByField;

    /**
     * 是否升序
     */
    private boolean isAsc;

    private final static String START = "start";
    private final static String LIMIT = "limit";

    /**
     * 设置 上一页
     */
    public void setUpPage() {
        this.upPage = (this.currentPage > 1) ? this.currentPage - 1 : this.currentPage;
    }

    /**
     * 设置下一页
     */
    public void setNextPage() {
        this.nextPage = (this.currentPage == this.totalPage) ? this.currentPage : this.currentPage + 1;
    }

    /**
     * 设置总页数
     */
    public void setTotalPage() {
        this.totalPage = (int) Math.ceil((double) totalCount / pageSize);
    }

    /**
     * 获取分页开始的位置(数据条数)
     */
    @Transient
    @JSONField(serialize = false)
    public int getBegin() {
        return this.getCurrentPage() < 1 ? 0 : (this.getCurrentPage() - 1) * this.getPageSize();
    }

    /**
     * 设置分页开始的位置(数据条数)
     */
    public void setBegin(int start) {
        setCurrentPage((int) Math.floor((double) start / pageSize) + 1);
    }

    /**
     * 获取分页结束的位置(数据条数)
     */
    @Transient
    @JSONField(serialize = false)
    public int getEnd() {
        return this.getCurrentPage() * this.getPageSize();
    }


    public static <T> PageList<T> build() {
        return new PageList<T>();
    }

    public static <T> PageList<T> build(T t) {
        return new PageList<T>(t);
    }

    public static <T> PageList<T> build(int currentPage, int pageSize) {
        return new PageList<T>(currentPage, pageSize);
    }

    public static <T> PageList<T> build(int currentPage, int pageSize, int totalCount) {
        return new PageList<T>(currentPage, pageSize, totalCount);
    }

    public static <T> PageList<T> build(int currentPage, int pageSize, int totalCount, java.util.List<T> records) {
        return new PageList<T>(currentPage, pageSize, totalCount, records);
    }

    public static <T> PageList<T> build(PageModel pageModel) {
        PageList<T> build = PageList.build();
        build.setCurrentPage(pageModel.getStart());
        build.setPageSize(pageModel.getLimit());
        build.setOrderByField(pageModel.getOrderBy());
        Boolean isAsc = pageModel.getIsAsc();
        if (null != isAsc) {
            build.setAsc(isAsc);
        }
        pageModel.setStart(build.getBegin());
        pageModel.setLimit(build.getPageSize());
        return build;
    }

    public static <T> PageList<T> build(Map params) {
        PageList<T> build = PageList.build();
        if (params.containsKey(START)) {
            build.setCurrentPage(Integer.valueOf(params.get(START).toString()));
        }
        if (params.containsKey(LIMIT)) {
            build.setPageSize(Integer.valueOf(params.get(LIMIT).toString()));
        }
        if (params.containsKey("orderBy")) {
            build.setOrderByField(params.get("orderBy").toString());
        }
        if (params.containsKey("isAsc")) {
            build.setAsc(Boolean.parseBoolean(params.get("isAsc").toString()));
        }
        params.put(START, build.getBegin());
        params.put(LIMIT, build.getPageSize());
        return build;
    }

    @Deprecated
    public static Map converter(Map params) {
        Integer pageSize = 10;
        if (params.containsKey(LIMIT)) {
            Integer limit = Integer.valueOf(params.get(LIMIT).toString());
            pageSize = limit < 1 ? 10 : limit;
            params.put(LIMIT, pageSize);
        }
        if (params.containsKey(START)) {
            Integer start = Integer.valueOf(params.get(START).toString()) / pageSize;
            params.put(START, start < 1 ? 1 : start + 1);
        }
        return params;
    }

    public PageList() {
    }

    public PageList(T t) {
        this.setData(t);
    }

    public PageList(int currentPage, int pageSize) {
        this.setCurrentPage(currentPage);
        this.setPageSize(pageSize);
    }

    public PageList(int currentPage, int pageSize, int totalCount) {
        this.setCurrentPage(currentPage);
        this.setPageSize(pageSize);
        this.setTotalCount(totalCount);
        init();
    }

    /**
     * 分页
     *
     * @param records     列表数据
     * @param totalCount  总记录数
     * @param pageSize    每页记录数
     * @param currentPage 当前页数
     */
    public PageList(int currentPage, int pageSize, int totalCount, java.util.List<T> records) {
        this.setCurrentPage(currentPage);
        this.setPageSize(pageSize);
        this.setTotalCount(totalCount);
        this.setRecords(records);
        init();

    }

    /**
     * 初始化计算分页
     */
    private void init() {
        this.setTotalPage();// 设置总页数
        this.setUpPage();// 设置上一页
        this.setNextPage();// 设置下一页
    }

    public String getOrderByField() {
        return orderByField;
    }

    public PageList<T> setOrderByField(String orderByField) {
        this.orderByField = orderByField;
        return this;
    }

    public boolean isAsc() {
        return isAsc;
    }

    public PageList<T> setAsc(boolean asc) {
        isAsc = asc;
        return this;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public PageList<T> setCurrentPage(int currentPage) {
        this.currentPage = currentPage < 1 ? 1 : currentPage;
        return this;
    }

    public int getPageSize() {
        return pageSize;
    }

    public PageList<T> setPageSize(int pageSize) {
        this.pageSize = pageSize < 1 ? 10 : pageSize;
        return this;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public PageList<T> setTotalCount(int totalCount) {
        this.totalCount = totalCount;
        this.setTotalPage();
        return this;
    }

    public java.util.List<T> getRecords() {
        return records;
    }

    public PageList<T> setRecords(java.util.List<T> records) {
        this.records = records;
        return this;
    }

    public PageList<T> setRecordsData(Map params, java.util.List<T> records) {
        setTotalCount(Integer.valueOf(params.get("count").toString()));
        setRecords(records);
        return this;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public T getData() {
        return data;
    }

    public PageList<T> setData(T data) {
        this.data = data;
        return this;
    }

    public int getUpPage() {
        return upPage;
    }

    public int getNextPage() {
        return nextPage;
    }
}
